//
//  CommonUtil.m
//  dongbo
//
//  Created by 葛殿宏 on 16/2/23.
//  Copyright © 2016年 arenacloud. All rights reserved.
//

#import "CommonUtil.h"
#import "sys/sysctl.h"
#import <CommonCrypto/CommonHMAC.h>

@implementation CommonUtil

#pragma mark - 路径获取
+ (NSString*) GetDocumentsPath;
{
    NSArray *paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                        NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    return documentsPath;
}


+ (BOOL)isEmptyString:(id)aString
{
    if ([[CommonUtil emptyString:aString] isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

+ (NSString *)emptyString:(id)aString{
    NSString *resultString = @"";
    if ([aString isKindOfClass:[NSNumber class]]) {
        resultString = [NSString stringWithFormat:@"%@",aString];
    }else if ([aString isKindOfClass:[NSNull class]])
    {
        resultString = @"";
    }
    else if (!aString || [aString length] == 0){
        
        resultString = @"";
    }
    else{
        resultString = aString;
    }
    return resultString;
}

+(NSString *)getIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL)
        {
            if(temp_addr->ifa_addr->sa_family == AF_INET)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
                {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
}

+ (NSString *)md5Encoding:(NSString*)aString
{
    if (!aString) {
        return nil;
    }
    const char *original_str = [aString UTF8String];
    unsigned char result[16];
    CC_MD5(original_str, (CC_LONG)strlen(original_str), result);
    NSData *md5Data = [NSData dataWithBytes:result length:16];
    return [CommonUtil convertDataToHexStr:md5Data];
}

+ (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}

@end
